package com.demo14;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;

public class CollectionsDemo1 {
    public static void main(String[] args) {
        //定义一个实例化对象信息ArrayList
        ArrayList<Student> arrayList = new ArrayList<Student>();

        //实例化学生对象增加到列表中去
        Student s1 = new Student("Kobe", 30);
        Student s2 = new Student("James", 24);
        Student s3 = new Student("Mark", 27);
        Student s4 = new Student("Army", 27);

        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        arrayList.add(s4);

        //开始进行排序
        Collections.sort(arrayList, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int num = s1.getAge() - s2.getAge();
                int num1 = num ==0 ? s1.getName().compareTo(s2.getName()) : num;
                return num1;
            }
        });

        //循环操作
        for (Student stu: arrayList){
            System.out.println(stu.getName()+"--"+stu.getAge());
        }
    }
}
